/**
 * @author 黄波
 * 创建日期：2011Oct 10, 2011
 * TODO// N/A
 */
package com.platform.njcc.action;

import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.platform.pojo.DownloadRecord;
import com.platform.pojo.ResInfo;
import com.platform.pojo.ResReviews;
import com.platform.pojo.UserInfo;
import com.platform.res.bo.ResBo;
import com.platform.user.bo.UserBo;

/**
 * 用户针对资源发表评论
 */
public class PublishReviewAction extends NJCCSuport {

	private String resId;	//资源ID
	private String comment;	//评论信息
	
	@Override
	public String execute() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		if(resId == null || resId.equals("")) {
			out.flush();
			out.write("<script>alert('资源为空！');history.back();</script>");
			out.close();
			return null;
		}
		//查询当前用户是否登录
		HttpSession session = ServletActionContext.getRequest().getSession();
		UserInfo user = (UserInfo) session.getAttribute("user");
		if(user == null) {
			out.flush();
			out.write("<script>alert('请先登录！');history.back();</script>");
			out.close();
			return null;
		}
		ResBo resBo = ResBo.getInstance();
		ResInfo res = resBo.getInfo(resId);
		if(res.getUploadUserId().intValue() == user.getId().intValue()) {
			out.flush();
			out.write("<script>alert('不能评论自已上传的资源！');history.back();</script>");
			out.close();
			return null;
		}
		//查看当前用户是否已评论过该资源
		ResReviews review = resBo.getResReview(user.getId().toString(), resId);
		if(review != null) {
			out.flush();
			out.write("<script>alert('不可重复评论！');history.back();</script>");
			out.close();
			return null;
		}
		if(comment == null || comment.trim().equals("")) {
			out.flush();
			out.write("<script>alert('评论信息不可为空！');history.back();</script>");
			out.close();
			return null;
		}
		//验证当前用户是否已下载过该资源
		DownloadRecord record = resBo.getDownloadRecord(user.getId(), Integer.parseInt(resId));
		if(record == null) {
			out.flush();
			out.write("<script>alert('请下载后评论！');history.back();</script>");
			out.close();
			return null;
		}
		//验证通过，进行评论保存
		ResReviews obj = new ResReviews();
		obj.setUserId(user.getId());
		obj.setResId(Integer.parseInt(resId));
		obj.setComment(comment);
		obj.setCommentTime(new Date());
		resBo.addResReview(obj);
		//评论成功后，更新评论人员信息（积分+1）
		user.setScore(user.getScore() + 1);
		UserBo.getInstance().updateUser(user);
		out.flush();
		out.write("<script>alert('评论成功！');history.back();</script>");
		out.close();
		return null;
	}
	public String getResId() {
		return resId;
	}
	public void setResId(String resId) {
		this.resId = resId;
	}
	public String getComment() {
		return comment;
	}
	public void setComment(String comment) {
		this.comment = comment;
	}
	
}
